<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>保密协议控制</title>
    <include file="common/tag"/>
    <style>
        body{
            background-color:#fff
        }
        .yui_lf{
            margin-left: 25px;
            margin-right: 20px;
            margin-top: 3px;
        }
        .yui_ck{
            opacity:1;
            position: absolute;
            z-index: 12;
            width: 18px;
            height: 18px;
            cursor: pointer;
        }
        .yui_lfl{
            margin-left: 20px;
        }
        .yui_top{
            margin-top: -20px;
        }
    </style>
</head>
<body>
    <input type="hidden" id="role_id" value="{$role_id}"/>
    <input type="hidden" name="__token__" value="{$Request.token}" />
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>
        <strong>模因警告!</strong>
        严禁未经授权的人员进行访问，肇事者将被监控，定位并处理!
        <br>
    </div>
    <div class="col-sm-12 yui_top">
        <h3 class="header smaller lighter red">
            <span >{$role_name}</span>
            <span class="yui_lfl">
                <button class="yui_check btn btn-warning">
                    <i class="icon-star align-top bigger-125"></i>
                    全选/反全选
                </button>
            </span>

            <span class="yui_lfl">
                <button class="yui_save btn btn-success">
                    <i class="icon-ok-sign align-top bigger-125"></i>
                    保存
                </button>
            </span>
        </h3>
        <foreach name="authorityArr" item="vo">
            <div class="well">
                <h4 class="green smaller lighter">{$vo.controllerName}</h4>
                    <foreach name="vo.child" item="vo2">
                        <div class="well">
                            <h4 class="blue smaller lighter"><span class="yui_label label label-xlg arrowed-in-right">{$vo2.name}</span></h4>
                            <foreach name="vo2" item="v" key="k">
                                <?php
                                if(isset($v)&&$k!='name'){
                            ?>
                                <neq name="v" value="0">
                                    <input id="{$vo.controller}-{$v}" name="form-field-checkbox" type="checkbox" class="yui_ck" value="{$vo.controller}-{$v}"><span class="yui_lf badge badge-success"><?php if(isset(explode('|',$k)[1])) { echo explode('|',$k)[1];} else { echo explode('|',$k)[0];}?></span>
                                </neq>
                                <?php
                             }
                            ?>
                            </foreach>
                        </div>
                    </foreach>
                </div>
        </foreach>

    </div>


</body>
    <script>
        $(function () {
            randow_label();
            fii_auth();

            /*
            * 全选与反全选
            * */
            var flag=true;
            $(".yui_check").on('click', function () {
                if (flag){
                    $("input[name='form-field-checkbox']:checkbox").each(function(){
                        $(this).prop("checked", true);
                    });
                    flag=false;
                }else{
                    $("input[name='form-field-checkbox']:checkbox").each(function() {
                        $(this).prop("checked", false);
                    });
                    flag=true;
                }
            });

            /*
            * 保存权限
            * */
            $(".yui_save").on('click', function () {
                var role_id=$('#role_id').val();
                //获取所有选中的checkbox
                var arr=new Array();
                $("input[type='checkbox']").each(function(i){
                    if($(this).is(':checked') == true){
                        if ($(this).val()!="on"){
                            var controller=$(this).val().split('-')[0];
                            var action=$(this).val().split('-')[1];
                            var subArr={};
                            subArr["controller"]=controller;
                            subArr["action"]=action;
                            var json=JSON.stringify(subArr)
                            arr.push(json)

                        }
                    }
                });
                var __token__=$("input[name='__token__']").val();
                //ajax提交数据
                $.post("/index.php/admin/role/role_authority",
                    {
                        role_id:role_id,
                        arr:arr,
                        __token__:__token__
                    },
                    function(data,status){
                        if (data['code']==1){
                            layer.open({
                                title: '提示',
                                content: '操作成功'
                                ,yes: function(index, layero){
                                    window.parent.location.reload();
                                },cancel: function(){
                                    window.parent.location.reload();
                                }
                            });
                        }else{
                            layer.msg(data['text']);
                        }
                    }, "json"
                );
            });
        });

        /*
        * 填充当前角色已有权限列表
        * */
        function fii_auth() {
            var pers=eval('{$has_pers}')
            for (i=0;i<pers.length;i++){
                $("#"+pers[i]).prop("checked", true);
            }
        }

        /*
        * 随机加载标签
        * */
        function randow_label() {
            var lables_info=['label-success','label-warning','label-danger','label-info','label-inverse'];
            var labels=$(".yui_label");
            for (i=0;i<labels.length;i++){
                $(labels[i]).addClass(lables_info[i%5]);
            }
        }
    </script>
</html>